package com.zjp.controller;

import com.zjp.pojo.DetectionPoint;
import com.zjp.pojo.NucleicTest;
import com.zjp.pojo.User;
import com.zjp.service.DetectionPointService;
import com.zjp.service.NucleicTestService;
import com.zjp.service.UserService;
import com.zjp.vo.NucleicAndDection;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 核酸检测
 */
@RestController
@Slf4j
public class NucleicTestController {
    @Autowired
    private NucleicTestService nucleicTestService;

    @Autowired
    private UserService userService;

    @Autowired
    private DetectionPointService detectionPointService;

    /**
     * 核酸检测结果
     * @param session
     * @return
     */
    @RequestMapping("/nucleicTest")
    public String nucleicTest(HttpSession session) {
        User user = (User) session.getAttribute("user");
        NucleicTest nucleicTest = new NucleicTest();
        nucleicTest.setUserid(user.getId());
        nucleicTest.setTime(new Date());
        int i = nucleicTestService.nucleicTest(nucleicTest);
        log.info("核酸检测结果：{}", i);
        if (i > 0) {
            return "核酸检测成功";
        }
        return "核酸检测失败";
    }

    /**
     * 我的检测记录
     */
    @RequestMapping("/myNucleicResult")    //我的检测记录
    public ModelAndView myNucleicResult(HttpSession session){
        User user= (User) session.getAttribute("user");
        ModelAndView mv=new ModelAndView();
        List<NucleicAndDection> nucleicTestList=nucleicTestService.selectMyNucleicResult(user.getId());
        log.info("我的检测记录：{}",nucleicTestList);
        mv.addObject("nucleicTestList",nucleicTestList);
        mv.setViewName("mynucleicresult");
        return mv;
    }

    /**
     * 核酸全部检测结果
     */
    @RequestMapping("/allNucleicResult")
    public ModelAndView allNucleicResult() {
        ModelAndView mv = new ModelAndView();
        List<NucleicAndDection> nucleicAndDectionList = nucleicTestService.selectAllNucleicResult();
        log.info("核酸全部检测结果：{}", nucleicAndDectionList);
        mv.addObject("nucleicAndDectionList", nucleicAndDectionList);
        mv.setViewName("allnucleicresult");
        return mv;
    }


//    --------------------------------------------------------------------------------------有Bug

    //查询用户全部核酸结果
    @RequestMapping("/findAllNucleicTestResult")
    @ResponseBody
    public Map findAllNucleicTestResult(String name,Date time,Integer result,Integer detectionid,HttpSession session){
        Map<String, Object> map=new HashMap();
        List<NucleicTest> nucleicTestList=nucleicTestService.selectNucleicTestResult(name,time,result,detectionid);
        map.put("nucleicTestList",nucleicTestList);
        return map;
    }



    /**
     * 查询核酸检测点
     */
    @RequestMapping("/findAlldetectionPointajax")
    public List<DetectionPoint> findAlldetectionPoint(){
        List<DetectionPoint> detectionPointList=detectionPointService.findAllDetectionPoint();
        return detectionPointList;
    }

    /**
     * 核酸检测结果录入
     * @param resultId
     * @param detectionid
     * @param result
     * @param userid
     * @return
     */
    @RequestMapping("/entryResult")
    public String entryResult(long resultId,long detectionid,byte result,Long userid){
        System.out.println("resultId:"+resultId+",detectionid:"+detectionid+",result:"+result+",userid:"+userid);
        if(result==1){
            //根据用户id修改用户健康码状态信息
            userService.updateHealthStatusByUserId(userid);
        }
        NucleicTest nucleicTest=new NucleicTest(resultId,new Date(),detectionid,result);
        // 录入核酸检测结果
        int i=nucleicTestService.entryResult(nucleicTest);
        if(i>0){
            return "SUCCESS";
        }
        return "error";
    }
}





